/**
 * Created by 白雨浓 on 2018/1/16 12:32.
 */

import {types} from './types';
import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';


export default{
    bean(self, table, fields){

        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'bean.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('bean模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        let clsName = self.$str.toCamel(table);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        let tmp = {
            packageName: self.config.packageName,
            author: self.config.author,
            date: self.$date.format(new Date(), 'yyyy/MM/dd hh:mm'),
            clsName: clsName,
            imports: new Set(),
            variables: []
        };


        for (const field of fields) {
            const fn = field.COLUMN_NAME;
            if (fn === 'id' || fn === 'state' || fn === 'create_time' || fn === 'update_time') {
                continue;
            }
            if (types[field.DATA_TYPE] === 'Date') {
                tmp.imports.add('java.util.Date');
            }

            tmp.variables.push({
                type: types[field.DATA_TYPE],
                name: self.$str.toCamel(field.COLUMN_NAME),
                comment: field.COLUMN_COMMENT !== null ? field.COLUMN_COMMENT : ''
            });
        }

        return ejs.render(tmpFile, tmp);
    }
    ,
}